package com.isecommerce.application.csvimporter;

import java.io.*;
import java.sql.*;

import com.isecommerce.framework.db.*;
import com.isecommerce.framework.util.*;
import com.isecommerce.application.csvimporter.entity.*;

public class CSVImporterDAO extends DataBase{
	
	private String configPath; 
	private Config impConfig;
	private DBInfoManager impDBM;
	private CSVFileInfoManager impCSVM;	 
   
    public CSVImporterDAO(String configPath){
    	
    	// Temporary Preset
    	this.configPath = configPath;    	
    	
    	// INITIALIZING
    	initConfig();
    }
    
    // Init
    private void initConfig(){
    	    	    	
    	printSepLine();
    	
    	System.out.println("\n[INIT CONFIG]");
    	
    	impConfig = new Config();
    	impConfig.webInit(configPath);
    	 	    	
    	System.out.println("\n[INIT DATABASE CONNECTION INFO]");
    	impDBM = new DBInfoManager();
    	impDBM.init(impConfig);
    	
    	System.out.println("\n[INIT CSV FILE META INFO]");
    	impCSVM = new CSVFileInfoManager();
    	impCSVM.init(impConfig);    	
    	    	
    }    
    
    // Main
    public void execute(){
    	
    	System.out.println("\n[EXECUTE CSV IMPORT OPERATION]");
    	
    	// Get CSV file count
    	int csvCnt = Integer.parseInt(impConfig.getValue("csv.count"));
    	
    	// Temp entity
    	CSVFileInfo fileInfo = null;
    	BufferedReader csvFile = null;    	
    	
    	Connection conn = null;    	
    	SubDAO subDao = null;	
    	
    	ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
    	    	    	    	    	    	   	
    	// Load CSV file(loop by csv.count)
    	for(int i=0; i < csvCnt; i++){
    		
    		
    		
    		
    		
    		// Get csv file info
    		fileInfo = impCSVM.getCSVfileInfoByIndex(i);    		
    		
    		// Process only enabled cSV info
    		if(fileInfo.enable.equals("true")){
    		
    			printSepLine();
    			System.out.println("Import process for -> " + fileInfo.csvname );
    		
    			try{
    			
    				// Read csv file with given encode value
    				csvFile = new BufferedReader(new InputStreamReader(new FileInputStream(fileInfo.filename),fileInfo.encode));
    				System.out.println("Loading file -> " + fileInfo.filename );
        		
    				// Switch Data Access by target table name
    				subDao = (SubDAO) myClassLoader.loadClass("com.isecommerce.application.csvimporter.subdao." + fileInfo.csvname).newInstance(); 
        		
    				// Open Connection by given dbinfo from csv file info
    				System.out.println("\nOpen connection....");
    				conn = getConnection(this.impDBM, fileInfo.dbname);
    				System.out.println("Connection Opened -> " + fileInfo.dbname );
                       		
    				// Run import
    				subDao.executeImport(csvFile, conn, fileInfo);
        		
    			}catch(IOException ioe){
    				System.out.println("[CSV_FILE_ERR] -> \n" + ioe.toString());
    			}catch(ClassNotFoundException cne){
    				System.out.println("[CLASS_NOT_FOUND] -> \n" + cne.toString());
    			}catch(SQLException se){
    				System.out.println("[SQL_CONNECTION_EXCEPTION] -> \n" + se.toString());
    			}catch(Exception e){
    				System.out.println("[ILLEGAL_EXCEPTION] -> \n");
    				e.printStackTrace();
    			}finally{    	
    				try{
    					if(csvFile != null) csvFile.close();    				
    				}catch(Exception e){    				
    				}
    			
    				try{
    					if(conn != null){
    						conn.close();
    						System.out.println("\nConnection Closed -> " + fileInfo.dbname );
    					}
    				}catch(Exception e){
    					System.out.println("[CONNECTION_CLOSE_EXCEPTION] -> \n");
    					e.printStackTrace();
    				}
    			}
    		
    		}
    		
        			
    	}
    	
    	
    }      
    
    public void printSepLine(){
    	System.out.println("\n--------------------------------------------------------------------------\n");
    }

}